package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.filehandlers.CBVMap;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.exceptions.InvalidFileException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

public class Map {
	public static final TextureInfoBean NULL = new TextureInfoBean("", "", "", -1);
	
	private String name;
	private String salt;
	
	private Chessboard arena;
	private HashMap<Integer, TextureInfoBean> textures;
	
	public Map()
	{
		textures = new HashMap<>();
//		textures.put(NULL.getAcutalKey(), NULL);
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the arena
	 */
	public Chessboard getArena() {
		return arena;
	}

	/**
	 * @return the hASHIDMAP
	 */
	public String getSalt() {
		return salt;
	}

	public void setSalt(final String salt)
	{
		this.salt = salt;
	}
	
	/**
	 * @return the textures
	 */
	public HashMap<Integer, TextureInfoBean> getTextures() {
		return textures;
	}
	
	/** */
	public void setArena(final Chessboard chessboard)
	{
		arena = chessboard;
	}
	
	/** */
	public void addTexture(final TextureInfoBean texture,final  boolean override)
	{
		final int key = texture.getAcutalKey();
		
		if(override || !textures.containsKey(key))
			textures.put(key, texture);
	}
	
	/** */
	public void setTextureToField(final int row, final int column, TextureInfoBean texture, final boolean override)
	{	
		addTexture(texture, override);
		arena.setTexture(row, column, texture);
	}
	
	/** */
	public void setTextureToField(final int row, final int column, final int textureKey)
	{
		arena.setTexture(row, column, textures.get(textureKey));
	}	
	
	public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFileException {
		CBVMap maphandler = new CBVMap();
		Map map = new Map();
		Chessboard board = new Chessboard(6, 6);
		for(int i = 0; i < 6; i++)
		{
			for(int j = 0; j < 6; j++)
				board.setWalkable(i, j, true);
		}
		
		TextureInfoBean y = new TextureInfoBean("first", "neutral", "png", 0);
		TextureInfoBean z = new TextureInfoBean("first", "nwalk", "png", 2);
		TextureInfoBean x = new TextureInfoBean("first", "ywalk", "png", 1);
		map.addTexture(x, true);
		map.addTexture(z, true);
		map.addTexture(y, true);
		map.setArena(board);
		map.setName("doomweek");
		map.setSalt("er");
		board.addSpawnCells(5, 5);
		board.addSpawnCells(5, 4);
		board.addSpawnCells(5, 3);
		board.addSpawnCells(5, 2);
		board.addSpawnCells(5, 1);
		board.addSpawnCells(5, 0);
		board.addSpawnCells(4, 5);
		board.addSpawnCells(4, 4);
		board.addSpawnCells(4, 3);
		board.addSpawnCells(4, 2);
		board.addSpawnCells(4, 1);
		board.addSpawnCells(4, 0);
//		for(int i = 0; i < 6; i++)
//			for(int j = 0; j < 6; j++)
//			{
////				board.setTexture(i, j, y);
//				map.setTextureToField(i, j, z, true);
//			}
		
		for(int i = 0; i < 6; i++)
		{
			for(int j = 0; j < 6; j++)
				map.setTextureToField(i, j, x, true);
		}
//		Map map = maphandler.build("cbvapps//maps", "first");
		
		
//		map.setTextureToField(0, 0, 2);
//		map.addTexture(texture, override);
//		map.setTextureToField(row, column, textureKey);
		maphandler.save("cbvapps//maps", map.getName(), map);
		System.out.println("end");
		
	}
}
